import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../../apis/alarm.dart';
import '../../../../components/date_range/date_range.dart';
import '../../../../components/status/toast.dart';
import '../../../../controller/gather_ctrl.dart';
import '../../../../controller/network/bridge.dart';
import '../../../../controller/network/network_ctrl.dart';
import '../../../../model/bean/alarm_bean.dart';

class HistoryAlarmCtrl extends GatherCtrl {
  // 小区名称
  String communityName = "";

  // 电梯的位置
  String liftLocation = "";

  // 查看网络状态
  final _nc = Get.find<NetworkCtrl>();

  // 标记符号（用于检查请求完成后无数据组件与数据组件之的切换）
  final MARKSYMBOL = false.obs;

  // 组件是否初始化完成
  RxBool isInitComplete = false.obs;

  // 日期区间
  final selectedDateRange = "请选择日期".obs;

  // 请求参数
  final Map<String, dynamic> params = {
    "elevatorId": "",
    "status": "",
    "startDate": "",
    "endDate": "",
    "page": 1,
    "rows": 20,
  }.obs;

  //申明上拉加载更多的状态
  final refreshController = RefreshController().obs;
  final flags = true.obs;

  // 数据列表
  final listData = <AlarmBean>[].obs;
  final total = 0.obs;

  @override
  void onInit() {
    // 创建网络变化观察
    Bridget.createNetworkCtrl(this);

    // 赋值电梯id
    params["elevatorId"] = Get.arguments["elevatorId"];

    // 赋值小区、电梯信息
    communityName = Get.arguments["communityName"];
    liftLocation = Get.arguments["liftLocation"];

    // 初始化页面
    readPageDataList();
  }

  @override
  void onClose() {
    // 移除观察
    Bridget.delNetworkCtrl(this);
  }

  @override
  void modify() {
    if (_nc.networkStatus.value) {
      if (listData.length <= 0) {
        readPageDataList(isToast: true);
      }
    }
  }

  /// 读取界面数据
  /// * [isToast] bool|是否展示动画框|选填|默认值：false
  /// * [isReset] bool|是否清空数据|选填|默认值：false
  /// * [callback] Function|回调函数|选填
  void readPageDataList({
    bool isToast = false,
    bool isReset = false,
    Function callback = null,
  }) async {
    // 开启轻提示
    if (isToast) Toast.loading(title: '正在读取');

    print(params);

    AlarmListBean result = await Alarm.getAlarmList(params);

    // 是否重置数据
    if (isReset) listData.clear();

    // 赋值
    if (result != null && result.list != null) {
      listData.addAll(result.list.obs);
      total.value = result.total;

      // 存在数据需要渲染
      if (result.list.length > 0) {
        MARKSYMBOL.value = true;
      } else {
        MARKSYMBOL.value = false;
      }

      // 累加页码
      if (result.list.length > 0 && listData.value.length > 0) {
        params["page"] = params["page"] + 1;
      }

      // 没有更多数据了
      if (listData.length >= total.value) {
        refreshController.value.loadNoData();
        flags.value = false;
      }
    }

    // 完成加载更多通知
    refreshController.value.loadComplete();

    // 开启轻提示
    if (isToast) Toast.closeToast();

    // 执行回调获取小区列表
    if (callback != null) await callback();

    // 标记页面加载状态（初始化）
    if (!isInitComplete.value) isInitComplete.value = true;
  }

  /// 清空日期选项
  void emptyDateRange() {
    if (selectedDateRange.value != "请选择日期") {
      // 清空日期选项
      selectedDateRange.value = "请选择日期";
      params["startDate"] = "";
      params["endDate"] = "";
      params["page"] = 1;
      flags.value = true;

      // 请求数据
      readPageDataList(isToast: true, isReset: true);
    }
  }

  /// 获取选中的日期区间
  /// * [context] BuildContext|上下文|必填
  void getDateRange(BuildContext context) {
    DateTimes.showDateRangePickers(
      context,
      firstDate: DateTime(1900),
      callBack: (DateTimeRangeResult dateRange) {
        // 执行请求参数
        params["startDate"] = dateRange.start;
        params["endDate"] = dateRange.end;
        params["page"] = 1;
        flags.value = true;

        // 设置界面展示状态
        selectedDateRange.value = "${dateRange.start} 至 ${dateRange.end}";

        // 请求数据
        readPageDataList(isToast: true, isReset: true);
      },
    );
  }

  /// 上拉加载更多
  void onLoading() {
    if (flags.value) {
      readPageDataList();
    } else {
      refreshController.value.loadNoData();
    }
  }
  //-
}
